package dao.impl;

import java.io.IOException;
import java.sql.*;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import util.*;
import dao.*;
import pojo.User;

public class UserDaoImpl implements UserDao {
    public Integer createTable() {
        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            UserDao mapper = session.getMapper(UserDao.class);
            Integer result = mapper.createTable();
            session.commit();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer dropTable() {
        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            UserDao mapper = session.getMapper(UserDao.class);
            Integer result = mapper.dropTable();
            session.commit();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<User> selectAll() {
        List<User> list = null;

        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            // User person = session.selectOne("dao.UserDao.selectById", 1);
            // 等价于
            UserDao mapper = session.getMapper(UserDao.class);
            list = mapper.selectAll();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    public User selectById(Integer id) {
        User person = null;

        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            // User person = session.selectOne("dao.UserDao.selectById", 1);
            // 等价于

            UserDao mapper = session.getMapper(UserDao.class);
            person = mapper.selectById(id);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return person;
    }

    public Integer deleteById(Integer id) {
        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            UserDao mapper = session.getMapper(UserDao.class);
            Integer result = mapper.deleteById(id);
            session.commit();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Integer insert(User user) {
        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            UserDao mapper = session.getMapper(UserDao.class);
            Integer result = mapper.insert(user);
            session.commit();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Integer update(User user) {
        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            UserDao mapper = session.getMapper(UserDao.class);
            Integer result = mapper.update(user);
            session.commit();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer inserts(List<User> users) {
        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            UserDao mapper = session.getMapper(UserDao.class);
            Integer result = mapper.inserts(users);
            session.commit();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer insertUser(User user) {
        try (SqlSession session = MybatisUtil.getSqlSessionByConfig()) {
            UserDao mapper = session.getMapper(UserDao.class);
            Integer result = mapper.insertUser(user);
            session.commit();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
